<template>
  <view class="mask">
    <view class="filter-scroll-view">
      <view class="filter-mask" @click="close"></view>
      <view>
        <scroll-view class="scroll-view-box" scroll-y="true">
          <view class="params-wrap">
            <view class="params-item-wrap" @click="$refs.deptSelectorRef.open()">
              <view class="left-content">所属部门</view>
              <view class="right-content">
                <view class="value">{{ formData.deptIdLabel }}</view>
                <u--image src="/static/images/study-garden/arrow-right.png" width="12rpx" height="20rpx"></u--image>
              </view>
            </view>
            <picker :range="typeList" range-key="businessType" @change="handlePickerChange">
              <view class="params-item-wrap">
                <view class="left-content">课件类别</view>
                <view class="right-content">
                  <view class="value">{{ formData.coursewareTypeLabel }}</view>
                  <u--image src="/static/images/study-garden/arrow-right.png" width="12rpx" height="20rpx"></u--image>
                </view>
              </view>
            </picker>
          </view>
        </scroll-view>
        <view class="btn-wrap">
          <view class="reset" @click="resetHandler">重置条件</view>
          <view class="confirm" @click="submitHandler">搜索</view>
        </view>
      </view>
    </view>

    <dept-selector ref="deptSelectorRef" @change="deptSelectorChange" :showBar="true"></dept-selector>
  </view>
</template>

<script>
import DeptSelector from '@/components/dept-selector/index.vue'
import { getCourseTypeList } from '@/api/studyGarden/index.js'

export default {
  components: {
    DeptSelector
  },
  data() {
    return {
      pickerShow: false,
      typeList: [],
      formData: {
        deptId: '',
        deptIdLabel: '',
        coursewareType: '',
        coursewareTypeLabel: '',
        coursewareName: ''
      }
    }
  },
  mounted() {
    this.getCourseTypeList()
  },
  onLoad(params) {
    this.formData.deptId = params.deptId || ''
    this.formData.deptIdLabel = params.deptIdLabel || ''
    this.formData.coursewareName = params.coursewareName || ''
    this.formData.coursewareType = params.coursewareType || ''
    this.formData.coursewareTypeLabel = params.coursewareTypeLabel || ''
  },
  methods: {
    handlePickerChange(target) {
      const selected = this.typeList[target.detail.value]
      this.formData.coursewareType = selected.typeId
      this.formData.coursewareTypeLabel = selected.businessType
    },
    close() {
      uni.navigateBack()
    },
    deptSelectorChange(item) {
      this.formData.deptId = item.value
      this.formData.deptIdLabel = item.name
    },
    async getCourseTypeList() {
      const res = await getCourseTypeList()
      this.typeList = res.data
    },
    resetHandler() {
      this.formData = {
        deptId: '',
        deptIdLabel: '',
        coursewareType: '',
        coursewareTypeLabel: '',
        coursewareName: ''
      }
      this.submitHandler()
    },
    submitHandler() {
      const eventChannel = this.getOpenerEventChannel()
      eventChannel.emit('callback:success', {
        data: this.formData
      })
      uni.navigateBack()
    }
  }
}
</script>

<style>
page {
  background: transparent;
}
</style>
<style lang="scss" scoped>
.placeholder-block {
  height: 84px;
}

.mask {
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  /* #ifndef APP-NVUE */
  display: flex;
  /* #endif */
  justify-content: center;
  align-items: center;
  background-color: rgba(0, 0, 0, 0.4);
}

.filter-scroll-view {
  width: 100vw;
  height: 100vh;
  display: flex;

  .filter-mask {
    flex: 1;
  }

  .scroll-view-box {
    padding-top: var(--status-bar-height);
    width: 672rpx;
    background: #fff;
    box-sizing: border-box;
    height: calc(100vh - 140rpx);

    .params-wrap {
      border-top: 1px solid #d5d7e3;
      border-bottom: 1px solid #d5d7e3;
      padding: 0 30rpx;

      .params-item-wrap {
        display: flex;
        align-items: center;
        justify-content: space-between;
        height: 112rpx;
        border-bottom: 1px solid #d5d7e3;
        font-size: 28rpx;
        color: #333333;

        &:last-child {
          border-bottom: none;
        }

        .left-content {
          font-weight: bold;
        }

        .right-content {
          display: flex;
          align-items: center;

          .value {
            margin-right: 16rpx;
          }
        }
      }
    }
  }

  .btn-wrap {
    background: #fff;
    display: flex;
    justify-content: space-around;
    padding-top: 30rpx;
    padding-bottom: calc(30rpx + constant(safe-area-inset-bottom));
    padding-bottom: calc(30rpx + env(safe-area-inset-bottom));

    .reset,
    .confirm {
      width: 292rpx;
      line-height: 80rpx;
      background: #ffffff;
      font-size: 32rpx;
      text-align: center;
    }

    .reset {
      color: #666666;
      background: #ffffff;
      border: 1rpx solid #cccccc;
    }

    .confirm {
      background: #2ac0c8;
      color: #fff;
    }
  }
}
</style>
